import Taro from '@tarojs/taro'
const { rpc, fromNowOn } = require('../../utils/util.js')
const service = require('./service.js')
const regeneratorRuntime = require('../../utils/async.js')
const app = module.exports = {};

app.wxlogin = async (params, action, state) => {
  const data = await rpc(service.wxlogin, params)
  const { is_admin, token } = data
  const { userInfo = {} } = Taro.getApp().globalData

  Taro.getApp().globalData.userInfo = { ...userInfo, ...data }
  Taro.getApp().globalData.token = token
  // app.globalData.token
}

app.getList = async (params, action, state) => {
  const { key, pageIndex = 1, pageSize = 10 } = params
  const { articles } = state
  const dataMap = result => result;
  const suggested = Taro.getStorageSync('artcleSuggested');
  const art = await rpc(
    service.getArticleV2, 
    { 
      filter_type: 'my_tags', 
      page: pageIndex, 
      per_page: pageSize, 
      last_suggested: suggested 
    }, 
    { dataMap }
  );
  const artList = art.data
  // console.log("artList 111111111111", artList)
  const total = artList.meta.number_of_update || 0

  articles[key] = articles[key] || {}
  articles[key].pageIndex = pageIndex
  articles[key].updateNum = total
  articles[key].showLoading = false
  articles[key].hasMore = pageSize * pageIndex < total
  articles[key].articles = [ ...(articles[key].articles || []), ...artList.articles ]

  if (articles[key].articles.length) {
    Taro.setStorageSync('artcleSuggested', articles[key].articles[0].suggested)
  }
  await action.setData({ articles })
  return articles
}

app.loadMore = async (params, action, state) => {
  const { articles } = state
  const { key } = params

  if (!articles[key].showLoading) {
    articles[key].showLoading = true
    await action.setData({ articles })
    await action.dispatch('getList', params)
  }
}

app.getMeAndTags = async (params, action, state) => {
  const me = await rpc(service.getMe)
  Taro.getApp().globalData.me = me
  if (!me.user_tags || me.user_tags.length === 0) {
    await action.dispatch('getTags')
  } else {
    await action.setData({ showTagPanel: false, myTags: ['我的', ...me.user_tags] })
  }
}

app.getArtsByTags = async (params, action, state) => {
  const { myTags } = state
  const [all, ...tags] = myTags

  // 不使用for循环，异步操作也不会出错
  await Promise.all(
    tags.map(key => action.dispatch('getList', { page: 1, key, tag: key }))
  )
}

app.getTags = async (params, action, state) => {
  console.log(1)
  const tags = await rpc(service.getTags);
  const { subs } = tags;
  // const tagOrginList = Object.keys(tags).map(tagName => {
  //   return { name: tagName }
  // })
  const extraLength = 5 - (subs.length % 5);
  const extraArray = [...new Array(extraLength)].map(() => ({
    pos: true,
    name: '占位'
  }));
  await action.setData({ subs: [...subs, ...extraArray], showTagPanel: true })
}

app.saveTags = async (params, action, state) => {
  const { tags } = params
  const me = await rpc(service.setMe, { tags: tags.join(',') })

  action.dispatch('getMeAndTags')
}
